package net.hhk.twitter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.hhk.connect.HTTPConnect;
import net.hhk.data.Constant;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity implements OnClickListener {
    
    EditText fullname,username,email,password,confirm_pass;
    private Pattern pattern;
    private Matcher matcher;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set View to register.xml
        setContentView(R.layout.register);
        username = (EditText) findViewById(R.id.reg_username);
        fullname = (EditText) findViewById(R.id.reg_fullname);
        email = (EditText) findViewById(R.id.reg_email);
        password = (EditText) findViewById(R.id.reg_password);
        confirm_pass = (EditText) findViewById(R.id.reg_confirmPassword);
        Button loginScreen = (Button) findViewById(R.id.btn_login);
        Button regis = (Button)findViewById(R.id.btnRegister);
        regis.setOnClickListener(this);
        
        // Listening to Login Screen link
        loginScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				// Switching to Login Screen/closing register screen
				finish();
			}
		});
    }
    public void EmailValidator(){
		  pattern = Pattern.compile(Constant.EMAIL_PATTERN);
	  }
    public boolean validate(final String hex){
    	 
		  matcher = pattern.matcher(hex);
		  return matcher.matches();

	  }
    public void onClick(View v){
    	switch (v.getId()) {
		case R.id.btnRegister:
			String name = username.getText().toString();
			String Email = email.getText().toString();
			String pass = password.getText().toString();
			String confirmpass = confirm_pass.getText().toString(); 
			String full = fullname.getText().toString();
			List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
			postParameters.add(new BasicNameValuePair("username", name));
			
			postParameters.add(new BasicNameValuePair("fullname", full));
			postParameters.add(new BasicNameValuePair("email",Email));
			postParameters.add(new BasicNameValuePair("password", pass));
			HTTPConnect http = HTTPConnect.getConnect();
			String result = http.doConnect(Constant.REGISTER, postParameters);
			TextView showResult = (TextView) findViewById(R.id.link_to_login);
			 try {
			      JSONObject obj = new JSONObject(result);
			      if(obj.getBoolean("flag")){
			    	  showResult.setTextColor(Color.BLUE);
			    	  showResult.setText(" Register Success");
			      }
			      else {
			    	  showResult.setTextColor(Color.RED);
			    	  showResult.setText(obj.getString("message"));
			      }
			    } catch (Exception e) {
			      e.printStackTrace();
			    }
			
			break;

		default:
			break;
		} 
     
    }
    public String checkRegister(String pass, String confirmPass, String email){
    	String result = "";
    	if(!pass.contentEquals(confirmPass)) {
    		result = "confirm password not match";
    		return result;
    	}else if(!validate(email)){
    		result = "don`t look like vaild email";
    	}
    	return result;
    }
}